
# REQUIRES: Data/Framing the Exit Experiment Raw Data.csv

################# Dependencies #################
# source("Load Packages.R")
# source("Analysis - Experiment/Cleaning.R")

################# Figure 3 - Attitudes Toward Withdrawal Across Treatment Conditions #################
### H1

# H1: Public support for withdrawal will be reduced when leaving is framed as a victory for the enemy in the conflict.
# logit withdraw_yn taliban
data$taliban_b <- ifelse(data$taliban == "2", 1, 0)
data$framing_b <- ifelse(data$framing == "2", 1, 0)

h1 <- glm (withdraw_yn ~ taliban, data = data, family="binomial")
summary(h1)
h1 %>% coef() %>% exp()

h1.1 <- glm (withdraw_yn ~ taliban_b, data = data, family="binomial")

data$t_frame <- data$taliban_b*data$framing_b

h1.2 <- glm (withdraw_yn ~ taliban_b + framing_b + t_frame, data = data, family="binomial")

h1_tab <- data.frame(taliban=0:1)
h1_tab <- cbind(h1_tab,predict(h1, newdata=h1_tab, type="response", se.fit=TRUE))
h1_tab$lower = h1_tab$fit - 1.96*h1_tab$se.fit
h1_tab$upper = h1_tab$fit + 1.96*h1_tab$se.fit
h1_tab

f2a <- h1_tab %>% dplyr::select(taliban, fit, lower, upper)
f2a$table <- "Enemy Victory Frame\nDV: Withdraw"
names(f2a)[names(f2a) == 'taliban'] <- 'binary'
f2a["binary"][f2a["binary"] == "0"] <- "No"
f2a["binary"][f2a["binary"] == "1"] <- "Yes"

logitmfx(withdraw_yn ~ taliban, data=data)

taliban <-  data[(data$taliban == "2"),]

### H2
# H2: Public support for staying the course in a conflict will go up when it is framed as a middling policy option rather than the most interventionist choice. 
# logit staycourse_yn framing
h2 <- glm (staycourse_yn ~ framing, data = data, family="binomial")
summary(h2)
h2 %>% coef() %>% exp()

h2.1 <- glm (staycourse_yn ~ framing_b, data = data, family="binomial")

h2.2 <- glm (staycourse_yn ~ taliban_b + framing_b + t_frame, data = data, family="binomial")

h2_tab <- data.frame(framing=0:1)
h2_tab <- cbind(h2_tab,predict(h2, newdata=h2_tab, type="response", se.fit=TRUE))
h2_tab$lower = h2_tab$fit - 1.96*h2_tab$se.fit
h2_tab$upper = h2_tab$fit + 1.96*h2_tab$se.fit
h2_tab

f2b <- h2_tab %>% dplyr::select(framing, fit, lower, upper)
f2b$table <- "Middle Ground Frame\nDV: Stay the Course"
names(f2b)[names(f2b) == 'framing'] <- 'binary'
f2b["binary"][f2b["binary"] == "0"] <- "No"
f2b["binary"][f2b["binary"] == "1"] <- "Yes"

f2 <- rbind(f2a, f2b)
rm(f2a, f2b)
f2$binary <- as.factor(f2$binary)

f2$fit_label <- paste0(format(round(f2$fit*100, 0), nsmall = 0), "%")

### Figure 3
margin <- unit(0.5, "line")
grob1 <- ggplot(f2, aes(x = factor(binary, level=c('No', 'Yes')), y = fit)) +
  geom_bar(aes(fill = fit), stat="identity", position="dodge", fill="#4292c6", width = 0.9) + 
  facet_wrap(.~table) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  facet_grid(~factor(table, levels=c('Enemy Victory Frame\nDV: Withdraw', 'Middle Ground Frame\nDV: Stay the Course')),scales="free") +
  scale_y_continuous(limits=c(0, 1), breaks = seq(0, 1, .2), 
                     label = percent) +
  labs(y = "Percent Support for Relevant Outcome",
       x = "Frame") +
  theme_minimal() +
  theme(legend.position = "none", text=element_text(family="Times New Roman")) +
  geom_label(data=f2, aes(label=fit_label, y=0.90), position=position_dodge(0.9), show.legend = FALSE, size=3, family = "Times New Roman")
grob2 <- textGrob("Figure 3: Attitudes Toward Withdrawal Across Treatment Conditions", gp=gpar(fontsize=13, fontfamily="Times New Roman"))

plot2 <- grid.arrange(grob1, grob2,
                      heights = unit.c(unit(1,"null"), grobHeight(grob2) + 1.2*margin),
                      vp=viewport(width=0.95, height=0.95))
ggsave("Plots/Figure 3 - Attitudes Toward Withdrawal Across Treatment Conditions.png", plot2)


grob1 <- ggplot(f2, aes(x = factor(binary, level=c('No', 'Yes')), y = fit)) +
  geom_bar(aes(fill = fit), stat="identity", position="dodge", fill="#4292c6", width = 0.9) + 
  facet_wrap(.~table) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  facet_grid(~factor(table, levels=c('Enemy Victory Frame\nDV: Withdraw', 'Middle Ground Frame\nDV: Stay the Course')),scales="free") +
  scale_y_continuous(limits=c(0, 1), breaks = seq(0, 1, .2), 
                     label = percent) +
  labs(y = "Percent Support for Relevant Outcome",
       x = "Frame") +
  theme_minimal() +
  theme(legend.position = "none", text=element_text(family="Times New Roman", size = 18),
        panel.background = element_rect(fill = 'white', colour = 'white'),
        plot.background = element_rect(fill = 'white', colour = 'white')) +
  geom_label(data=f2, aes(label=fit_label, y=0.90), position=position_dodge(0.9), show.legend = FALSE, size=5, family = "Times New Roman")
ggsave("Plots/Silverman POQ-23-0350.R1 Figure 3.eps", grob1)

rm(f2, h1, h1_tab, h1.1, h1.2, h2, h2_tab, h2.1, h2.2, taliban, grob1, grob2, plot2, margin)
